package cn.edu.lingnan.tourism.controller;

import cn.edu.lingnan.tourism.exeption.TourismException;
import cn.edu.lingnan.tourism.pojo.Route;
import cn.edu.lingnan.tourism.pojo.Seller;
import cn.edu.lingnan.tourism.pojo.User;
import cn.edu.lingnan.tourism.service.AdminService;
import cn.edu.lingnan.tourism.service.RouteService;
import cn.edu.lingnan.tourism.service.SellerService;
import cn.edu.lingnan.tourism.service.UserService;
import cn.edu.lingnan.tourism.utils.Md5Util;
import cn.edu.lingnan.tourism.utils.Result;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;
    @RequestMapping("/getAllUsers/{page}")
    public String getAllUsers(@PathVariable("page")Integer page){//查询操作
        try {
            PageHelper.startPage(page,9);
            List<User> users = adminService.getAllUsers();
            return new Result<>().success(new PageInfo(users));
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/getAllSellers/{page}")
    public String getAllSellers(@PathVariable("page")Integer page){//查询操作
        try {
            PageHelper.startPage(page,9);
            List<Seller> sellers = adminService.getAllSellers();
            return new Result<>().success(new PageInfo(sellers));
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/getAllRoutes/{page}")
    public String getAllRoutes(@PathVariable("page")Integer page){//查询操作
        try {
            PageHelper.startPage(page,9);
            List<Route> routes = adminService.getAllRoutes();
            return new Result<>().success(new PageInfo(routes));
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    //------------------------------------------------------------------
    @RequestMapping("/deleteUser/{uid}")
    public String deleteUser(@PathVariable("uid") Integer uid){//删除操作
        try {
            int flag = adminService.deleteUser(uid);
            return new Result<>().success("删除成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/deleteSeller/{sid}")
    public String deleteSeller(@PathVariable("sid") Integer sid){//删除操作
        try {
            int flag = adminService.deleteSeller(sid);
            return new Result<>().success("删除成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/deleteRoute/{rid}")
    public String deleteRoute(@PathVariable("rid") Integer rid){//删除操作
        try {
            int flag = adminService.deleteRoute(rid);
            return new Result<>().success("删除成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }

    //-------------------------------------------------------------------------
    @RequestMapping("/updateUser")
    public String updateUser(@RequestBody User user){//更新操作
        try {
            int flag = adminService.updateUser(user);
            return new Result<>().success("更新成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/updateSeller")
    public String updateSeller(@RequestBody Seller seller){//更新操作
        try {
            int flag = adminService.updateSeller(seller);
            return new Result<>().success("更新成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/updateRoute")
    public String updateRoute(@RequestBody Route route){//更新操作
        try {
            int flag = adminService.updateRoute(route);
            return new Result<>().success("更新成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    //----------------------------------------------------------------
    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){//添加操作
        try {
            user.setPassword(Md5Util.encodeByMd5(user.getPassword()));
            int flag = adminService.addUser(user);
            return new Result<>().success("添加成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/addSeller")
    public String addSeller(@RequestBody Seller seller){//添加操作
        try {
            int flag = adminService.addSeller(seller);
            return new Result<>().success("添加成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/addRoute")
    public String addRoute(@RequestBody Route route){//添加操作
        try {
            route.setRimage("home-01.jpg");
            int flag = adminService.addRoute(route);
            return new Result<>().success("添加成功",flag);
        }catch (TourismException e){
            return new Result<>().error(e.getMessage());
        }
    }
    @RequestMapping("/user/search/{page}")
    public String searchUser(@PathVariable("page")Integer page,@RequestBody String search){
        JSONObject parse = (JSONObject) JSON.parse(search);
        String searchStr = (String) parse.get("search");
        String trim = searchStr.trim();
        PageHelper.startPage(page,9);
        List<User> users = adminService.selectUserByString(trim);
        return new Result<>().success(new PageInfo(users));
    }
    @RequestMapping("/route/search/{page}")
    public String searchRoute(@PathVariable("page")Integer page,@RequestBody String search){
        JSONObject parse = (JSONObject) JSON.parse(search);
        String searchStr = (String) parse.get("search");
        String trim = searchStr.trim();
        PageHelper.startPage(page,9);
        List<Route> routes = adminService.selectRouteByString(trim);
        return new Result<>().success(new PageInfo(routes));
    }
    @RequestMapping("/seller/search/{page}")
    public String searchSeller(@PathVariable("page")Integer page,@RequestBody String search){
        JSONObject parse = (JSONObject) JSON.parse(search);
        String searchStr = (String) parse.get("search");
        String trim = searchStr.trim();
        PageHelper.startPage(page,9);
        List<Seller> sellers = adminService.selectSellerByString(trim);
        return new Result<>().success(new PageInfo(sellers));
    }
    @RequestMapping("/getCount")
    public String getCount(){
        Map<String, Integer> allNumber = adminService.getAllNumber();
        return new Result<>().success(allNumber);
    }
}
